<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>添加模块 - 管理界面</title>
    <style>
        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 2rem;
        }

        .section-header {
            margin-bottom: 2rem;
        }

        .form-container {
            background-color: #fff;
            padding: 2rem;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .form-group {
            margin-bottom: 1.5rem;
        }

        .form-group label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: 600;
            color: #333;
        }

        .form-group input {
            width: 100%;
            padding: 0.75rem;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 1rem;
        }

        .form-group input:focus {
            outline: none;
            border-color: #007bff;
            box-shadow: 0 0 0 2px rgba(0,123,255,0.25);
        }

        .form-actions {
            display: flex;
            gap: 1rem;
            justify-content: flex-end;
            margin-top: 2rem;
        }

        .btn {
            display: inline-block;
            padding: 0.5rem 1rem;
            border-radius: 4px;
            text-decoration: none;
            cursor: pointer;
            border: none;
            font-size: 1rem;
        }

        .btn-primary {
            background-color: #007bff;
            color: white;
        }

        .btn-secondary {
            background-color: #6c757d;
            color: white;
        }

        .alert {
            padding: 1rem;
            margin-bottom: 1rem;
            border-radius: 4px;
        }

        .alert-success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .alert-danger {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="section-header">
            <h1>添加模块</h1>
        </div>
        
        <form id="addModForm" class="form-container">
            <div class="form-group">
                <label for="modid">模块ID</label>
                <input type="number" id="modid" name="modid" required>
            </div>
            
            <div class="form-group">
                <label for="content">模块说明</label>
                <input type="text" id="content" name="content" required>
            </div>
            
            <div class="form-actions">
                <a href="/db/modules" class="btn btn-secondary">取消</a>
                <button type="submit" class="btn btn-primary">保存</button>
            </div>
        </form>
    </div>

    <script>
        document.getElementById('addModForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const modid = document.getElementById('modid').value;
            const content = document.getElementById('content').value;
            
            try {
                const response = await fetch('/api/modules', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ modid, content })
                });
                
                const data = await response.json();
                if (data.status === 'success') {
                    showSuccess('添加成功');
                    setTimeout(() => {
                        window.location.href = '/db/modules';
                    }, 1500);
                } else {
                    showError(data.message || '添加失败');
                }
            } catch (error) {
                console.error('添加模块失败:', error);
                showError('添加失败');
            }
        });

        // 显示成功消息
        function showSuccess(message) {
            const alert = document.createElement('div');
            alert.className = 'alert alert-success';
            alert.textContent = message;
            document.querySelector('.section-header').insertAdjacentElement('afterend', alert);
            setTimeout(() => alert.remove(), 3000);
        }

        // 显示错误消息
        function showError(message) {
            const alert = document.createElement('div');
            alert.className = 'alert alert-danger';
            alert.textContent = message;
            document.querySelector('.section-header').insertAdjacentElement('afterend', alert);
            setTimeout(() => alert.remove(), 3000);
        }
    </script>
</body>
</html>